package activemq.jms.queue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Consumer {
	private static final String URL = "tcp://127.0.0.1:61616";

	private static final String QNAME = "Q-text";

	public static void main(String[] args) throws JMSException {
		// 1、创建连接工厂
		ConnectionFactory factory = new ActiveMQConnectionFactory(URL);

		// 2、创建连接
		Connection conn = factory.createConnection();

		// 3、启动连接
		conn.start();

		// 4、创建回话
		Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

		// 5、创建目的地
		Destination destination = session.createQueue(QNAME);
		
		//6、创建消费者
		MessageConsumer consumer = session.createConsumer(destination);
		
		//7、监听消息
		consumer.setMessageListener((message)->{
			try {
				System.out.println(((TextMessage)message).getText());
			} catch (JMSException e) {
				e.printStackTrace();
			}
		});
	}

}
